Certified Java Programmer Mock Exam


Question 1


class E {
  public static void main (String args[]) {
    int i1 = 0xffffffff;
    int i2 = i1 << 1;
    int i3 = i1 >> 1;
    int i4 = i1 >>> 1;
    System.out.print(Integer.toHexString(i2) + ",");
    System.out.print(Integer.toHexString(i3) + ",");
    System.out.print(Integer.toHexString(i4));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: ffffffff,ffffffff,ffffffff
b. Prints: ffffffff,ffffffff,7fffffff
c. Prints: ffffffff,7fffffff,ffffffff
d. Prints: ffffffff,7ffffffe,7ffffffe
e. Prints: fffffffe,ffffffff,ffffffff
f. Prints: fffffffe,ffffffff,7fffffff
g. Prints: fffffffe,7fffffff,ffffffff
h. Prints: fffffffe,7fffffff,7fffffff
i. Runtime error
j. Compiler error
k. None of the above

Question 2


class D {
  public static void main (String args[]) {
    int i1 = ~1;
    int i2 = -1;
    int i3 = -2;
    System.out.print(Integer.toHexString(i1) + ",");
    System.out.print(Integer.toHexString(i2) + ",");
    System.out.print(Integer.toHexString(i3));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: ffffffff,ffffffff,ffffffff
b. Prints: ffffffff,ffffffff,fffffffe
c. Prints: ffffffff,fffffffe,ffffffff
d. Prints: ffffffff,fffffffe,fffffffe
e. Prints: fffffffe,ffffffff,ffffffff
f. Prints: fffffffe,ffffffff,fffffffe
g. Prints: fffffffe,fffffffe,ffffffff
h. Prints: fffffffe,fffffffe,fffffffe
i. Runtime error
j. Compiler error
k. None of the above

Question 3


class V {
  public static void main (String[] args) {
    System.out.print((0.0 % -1)+","+(-0.0 % 1)+","+(-0.0 % -1));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 0.0,0.0,0.0
b. Prints: 0.0,0.0,-0.0
c. Prints: 0.0,-0.0,0.0
d. Prints: 0.0,-0.0,-0.0
e. Prints: -0.0,0.0,0.0
f. Prints: -0.0,0.0,-0.0
g. Prints: -0.0,-0.0,0.0
h. Prints: -0.0,-0.0,-0.0
i. Runtime error
j. Compiler error
k. None of the above

Question 4


class Color {}
class Red extends Color {}
class Blue extends Color {}
class A {
  public static void main (String[] args) {
    Color c1 = new Red();
    Color c2 = new Blue();
    Red r1 = new Red();
    boolean b1 = c1 instanceof Color;
    boolean b2 = c1 instanceof Blue;
    boolean b3 = r1 instanceof Blue;
    System.out.print(b1+","+b2+","+b3);
  }
}

What is the result of attempting to compile and run the above program?

a. false,false,false
b. false,false,true
c. false,true,false
d. false,true,true
e. true,false,false
f. true,false,true
g. true,true,false
h. true,true,true
i. Runtime error
j. Compiler error
k. None of the above

Question 5


class W {
  public static void main (String[] args) {
    byte x = 3;
    byte y = 5;
    System.out.print((-x == ~x + 1)+","+(-y == ~y + 1));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: false,false
b. Prints: false,true
c. Prints: true,false
d. Prints: true,true
e. Runtime error
f. Compiler error
g. None of the above

Question 6


class A {
  public static void main (String[] args) {
    int a = 1 | 2 ^ 3 & 5;
    int b = ((1 | 2) ^ 3) & 5;
    int c = 1 | (2 ^ (3 & 5));
    System.out.print(a + "," + b + "," + c);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 0,0,0
b. Prints: 0,0,3
c. Prints: 0,3,0
d. Prints: 0,3,3
e. Prints: 3,0,0
f. Prints: 3,0,3
g. Prints: 3,3,0
h. Prints: 3,3,3
i. Runtime error
j. Compiler error
k. None of the above

Question 7


class V {
  public static void main (String[] args) {
    System.out.print(Float.NaN % 2 + ",");
    System.out.print(2 % Float.NaN);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: NaN,NaN
b. Prints: NaN,2.0
c. Prints: 2.0,NaN
d. Prints: 2.0,2.0
e. Runtime error
f. Compiler error
g. None of the above

Question 8


class L {
  public static void main (String s[]) {
    int i = 1 | 2 ^ 15 & 7 ^ 13 | 2;
    System.out.println(i % 5);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 1
b. Prints: 2
c. Prints: 3
d. Prints: 4
e. Prints: 5
f. Runtime error
g. Compiler error
h. None of the above

Question 9


class Chartreuse {
  public static void main (String[] args) {
    short i = (short)Float.NEGATIVE_INFINITY;
    short j = (short)Float.POSITIVE_INFINITY;
    short k = (short)Float.NaN;
    System.out.println((i == Short.MIN_VALUE) +
      ", " + (j == Short.MAX_VALUE) +
      ", " + (k == 0));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: false, false, false
b. Prints: false, false, true
c. Prints: true, true, false
d. Prints: true, true, true
e. Compiler error.
f. Run time error.

Question 10


class B {
  static boolean m(boolean b) {
    System.out.print(b + " ");
    return b;
  }
  public static void main(String[] args) {
    boolean a = false;
    boolean b = false;
    boolean c = true;
    boolean d = false;

    m(m(a | b == c & d) == m((a | b) == (c & d)));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: true false false
b. Prints: false true false
c. Prints: true true true
d. Prints: false false true
e. Runtime error.
f. Compiler error.
g. None of the Above

Question 11


class A {}
class B extends A {}
class C extends B {
  static void m(A x, A y) {System.out.print("AA");}
  static void m(A x, B y) {System.out.print("AB");}
  static void m(B x, A y) {System.out.print("BA");}
  static void m(B x, B y) {System.out.print("BB");}
  static void m(A x, C y) {System.out.print("AC");}
  public static void main(String[] args) {
    C c = new C();
    m(c, c);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: AA
b. Prints: AB
c. Prints: BA
d. Prints: BB
e. Prints: AC
f. Compiler error.
g. Runtime error.
h. None of the Above

Question 12


class A {
  public static void main (String[] args) {
    System.out.print((new Object() instanceof Object)+",");
    System.out.print((new Object() instanceof String)+",");
    System.out.print((new String() instanceof Object));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: false,false,false
b. Prints: false,false,true
c. Prints: false,true,false
d. Prints: false,true,true
e. Prints: true,false,false
f. Prints: true,false,true
g. Prints: true,true,false
h. Prints: true,true,true
i. Runtime error
j. Compiler error
k. None of the above

Question 13


1.  class Primitives {
2.    static void printFloat(float f) {System.out.println(f);}
3.    static void printDouble(double d) {System.out.println(d);}
4.    public static void main(String[] args) {
5.      byte b = 1;
6.      short s = b;
7.      char c = s;
8.      int i = c;
9.      long l = i;
10.     float f = l;
11.     printFloat(i);
12.     printFloat(l);
13.     printDouble(l);
14.   }
15. }

What is the result of attempting to compile and run the above program?

a. Compiler error at line 3.
b. Compiler error at line 4.
c. Compiler error at line 5.
d. Compiler error at line 6.
e. Compiler error at line 7.
f. Compiler error at line 8.
g. Compiler error at line 9.
h. Compiler error at line 10.
i. Compiler error at line 11.
j. Compiler error at line 12.
k. Compiler error at line 13.
l. Compiles and runs without error.

Question 14


class P{
  public static void main (String[] s) {
    byte b = 5;
    System.out.println(b<<33);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: -1
b. Prints: 0
c. Prints: 1
d. Prints: 5
e. Prints: 10
f. Runtime error
g. Compiler error
h. None of the above

Question 15


class L {
  static int m(int i) {
    System.out.print(i + ", ");
    return i;
  }

  public static void main(String s[]) {
    m(m(1) - m(2) + m(3) * m(4));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 1, 2, 3, 4, 8,
b. Prints: 1, 2, 3, 4, 11,
c. Prints: 3, 4, 1, 2, 11,
d. Runtime error
e. Compiler error
f. None of the above

Question 16


class K {
  public static void main (String[] s) {
    byte b = 127;
    b <<= 2;
    System.out.println(b);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: -4
b. Prints: -3
c. Prints: -2
d. Prints: 0
e. Prints: 1
f. Prints: 127
g. Prints: 508
h. Runtime Exception
i. Compiler Error
j. None of the Above

Question 17


class M {
  static int m(int i) {
    System.out.print(i + ", ");
    return i;
  }

  public static void main(String s[]) {
    m(m(1) + m(2) % m(3) * m(4));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 1, 2, 3, 4, 0,
b. Prints: 1, 2, 3, 4, 3,
c. Prints: 1, 2, 3, 4, 9,
d. Prints: 1, 2, 3, 4, 12,
e. Prints: 2, 3, 4, 1, 9,
f. Prints: 2, 3, 4, 1, 3,
g. Runtime error
h. Compiler error
i. None of the above


Copyright © 2002, Dan Chisholm
All rights reserved.
1